package ktarou.m1k4.icu.window;

import ktarou.m1k4.icu.handler.UploadHandler;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class LoginWindow {
	private Button loginButton;
	private Button cancelButton;
	private Text usernameText;
	private Text passwordText;
	private Label usernameLabel;
	private Label passwordLabel;
	private Display display;
	private Shell shell;

	public LoginWindow() {
		this.display = Display.getCurrent();
		shell = new Shell(display, SWT.CLOSE | SWT.MIN);
		init();
	}

	private void init() {

		shell.setLayout(new GridLayout(2, true));
		//shell.setSize(154, 120);
		shell.setSize(600, 200);
		Monitor primary = display.getPrimaryMonitor();
		Rectangle bounds = primary.getBounds();
		Rectangle rect = shell.getBounds();
		System.out.println("bounds : " + bounds.width + "X" + bounds.height);
		System.out.println("rect : " + rect.width + "X" + rect.height);
		int x = bounds.x + (bounds.width - rect.width) / 2;
		int y = bounds.y + (bounds.height - rect.height) / 2;
		shell.setLocation(x, y);

		usernameLabel = new Label(shell, SWT.LEFT);
		usernameLabel.setText("Username");
		usernameLabel.pack();
		usernameText = new Text(shell, SWT.LEFT);
		usernameText.pack();

		passwordLabel = new Label(shell, SWT.LEFT);
		passwordLabel.setText("Password");
		passwordLabel.pack();
		passwordText = new Text(shell, SWT.LEFT);
		passwordText.pack();

		loginButton = new Button(shell, SWT.PUSH);
		loginButton.setText("Login");
		loginButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				if (!usernameText.getText().equalsIgnoreCase("")
						&& !passwordText.getText().equalsIgnoreCase("")) {
					UploadHandler uh = UploadHandler.getInstance();
					uh.setUsername(usernameText.getText());
					uh.setPassword(passwordText.getText());
					if (uh.login()) {
						event.display.getActiveShell().setVisible(false);
						new UploadWindow();
					} else {
						System.out.println("Koneksi Gagal");
					}
				}else{
					MessageBox messageBox = new MessageBox(shell, SWT.ICON_ERROR | SWT.OK); 
	                messageBox.setMessage("Username ama Password diisi dong!");
	                messageBox.open( );
				}
			}
		});
		loginButton.pack();

		cancelButton = new Button(shell, SWT.PUSH);
		cancelButton.setText("Cancel");
		cancelButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				System.out.println("Cancel Pressed");
			}
		});
		cancelButton.pack();

		shell.pack();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

	public Button getLoginButton() {
		return loginButton;
	}

	public void setLoginButton(Button loginButton) {
		this.loginButton = loginButton;
	}

	public Button getCancelButton() {
		return cancelButton;
	}

	public void setCancelButton(Button cancelButton) {
		this.cancelButton = cancelButton;
	}

	public Text getUsernameText() {
		return usernameText;
	}

	public void setUsernameText(Text usernameText) {
		this.usernameText = usernameText;
	}

	public Text getPasswordText() {
		return passwordText;
	}

	public void setPasswordText(Text passwordText) {
		this.passwordText = passwordText;
	}

	public Label getUsernameLabel() {
		return usernameLabel;
	}

	public void setUsernameLabel(Label usernameLabel) {
		this.usernameLabel = usernameLabel;
	}

	public Label getPasswordLabel() {
		return passwordLabel;
	}

	public void setPasswordLabel(Label passwordLabel) {
		this.passwordLabel = passwordLabel;
	}

	public Display getDisplay() {
		return display;
	}

	public void setDisplay(Display display) {
		this.display = display;
	}

	public Shell getShell() {
		return shell;
	}

	public void setShell(Shell shell) {
		this.shell = shell;
	}

}
